﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Entity;


namespace Estimate.Reports
{
  public  class StationsOfLackScheduleReport
    {
        AffiliationRepository affRepository;
        ScheduleRepository schRepository;

        AffiliationEntity[] needStations;
        Dictionary<string,int> existStations;
        int days = 0;

        public StationsOfLackScheduleReport(string connectionString)
        {
            affRepository = new AffiliationRepository(connectionString);
            schRepository = new ScheduleRepository(connectionString);
        }

        public void PrepareData(DateTime beginDate, DateTime endDate)
        {
            needStations = affRepository.GetStationLevel(4);//首先找出所有4级站的站号
            existStations = schRepository.GetScheduleCountWithDateRange(beginDate, endDate);//获取该时间段内所有站点的排班表记录数
            days = endDate.Subtract(beginDate).Days + 1;//该时间段内的天数
        }

        public List<string> GetStationsOfLackSchedule()
        {
            List<string> lackStation = new List<string>(needStations.Length);
            Dictionary<string ,string> needStationsName=new Dictionary<string,string>();

            //查找没有排班表记录的站点
            foreach (AffiliationEntity entity in needStations)
            {
                needStationsName.Add(entity.code,entity.name);

                if (!existStations.ContainsKey(entity.code))
                    lackStation.Add(entity.name);
            }
            //查找排班表记录数少于3*days的站点
            foreach (KeyValuePair<string, int> pair in existStations)
            {
                if (pair.Value < 3 * days)
                    lackStation.Add(needStationsName[pair.Key]);
            }
            return lackStation;
        }

    }
}
